
import json
from spider_nga.nga import stand_v2
from spider_nga.snowball import mian_do
from spider_xue.stocks import run, stock
from spider_xue.account import saveAccount, update_account
import random
import requests
import time
import numpy as np

import decimal

from spider_xue.settings import KEYS, URL5, COOKIES, ding_url1, ding_url0
from spider_xue.thread_file import mission_day_all_stock, mission_five
from spider_xue.utils import EMA, break_flag, get_record_datasad, get_record_histroy, get_record_histroy1, get_record_histroy2, get_record_histroy3, get_record_histroy4, reset_all_ip, send_msg, send_s_msg, get_record_histroy5
from db import *
from spider_xue.ip import headers_list, ip_obj
from spider_xue.new_ip import check_duplicate_ip, clean_db_ip, clean_db_txt_ip, clean_dirty_db_ip, new_ip_obj
from spider_xue.wx_push import send_info
from spider_xue.get_day import do_day, set_all_stock_tmp


def update_his(his, symbol, key):
    his.update_one({"symbol": symbol}, {"$set": key}, upsert=True)

def set_five_history():
    test_data = mydb['history_tmp'].find()
    count = 0
    for data in test_data:
        item = data['item']
        print(data['symbol'], count)
        
        history = history_five_db.find_one({"symbol": data['symbol']})
        if not history:
            five_time_list = []
            five_time_dict = {}
            fifteen_time_list = []
            fifteen_time_dict = {}

        else:
            five_time_list = history['five_time_list']
            five_time_dict = history['five_time_dict']
            fifteen_time_list = history.get('fifteen_time_list', [])
            fifteen_time_dict = history.get('fifteen_time_dict', {})
        
        r = len(item)
        flag15, tmp15 = 0, 0
    
        for i in range(r):
            d = item[i]
            tt = time.localtime(int(d[0] / 1000))
            itt = time.strftime('%Y-%m-%d', tt)
            
            set_value_five(five_time_list, five_time_dict, itt, data['symbol'], d[9])

            if tt.tm_min in [5, 20, 35, 50]:
                set_value_five(fifteen_time_list, fifteen_time_dict, itt, data['symbol'], tmp15)
                flag15 = 3
                tmp15 = 0
            if flag15:
                flag15 -= 1
                tmp15 += d[9]

        update_his(
            history_five_db, data['symbol'], 
            {
                "five_time_list": five_time_list,
                "five_time_dict": five_time_dict,
                "fifteen_time_list": fifteen_time_list,
                "fifteen_time_dict": fifteen_time_dict,
            }
        )

def set_value_five(time_list, time_dict, itt, symbol, cal, ti=None, ttt=None):
    if time_list:
        if itt not in time_list:
            if itt > time_list[-1]:
                time_dict[itt] = cal
                time_list.append(itt)
            else:
                send_msg("### 时间不对拉！！！尼玛 %s %s" % (itt, symbol))
        else:
            if time_dict[itt] < cal:
                time_dict[itt] = cal
    else:
        time_dict[itt] = cal
        time_list.append(itt)

def fix():
    test_data = []

    for x in ['SZ002515', 'SH603233']:
        for i in range(50):
            iip = new_ip_db.find_one()
            try:
                tttt = get_res(COOKIES, x, iip['ip'])
                break
            except:
                new_ip_db.delete_many({"ip": iip['ip']})
                print('fail....', iip['ip'])
        test_data.append(tttt)
    mydb['history_tmp'].insert_many(test_data)
    set_five_history()

def get_res(cookie, symbol, ippp):
    tt = str(int(time.time())) + "000"
    ss = requests.Session()
    proxies = {'http': f'http://{ippp}'}
    ss.headers = {"user-agent": random.choice(headers_list), "cookie": cookie}
    html = ss.get(URL5.format(symbol, tt, 1000), proxies=proxies, timeout=5)
    if html.status_code != 200:
        raise ValueError("not 200")
    tempJson = html.json()
    if not tempJson.get('data'):
        raise ValueError('not data')
    return tempJson.get('data')

def set_ip_count():
    iipp = mydb['new_ip']
    iipp.update_many({}, {"$set": {"status": 1, "count": 2}})

def get_name():
    l = history_five_db.find()
    for i in l:
        f = stock_db.find_one({'symbol': i['symbol']})
        history_five_db.update_one({'symbol': i['symbol']}, {"$set": {"name": f['name']}})
        print(f['name'], i['symbol'])

def test_son_work_together_one_mintue():
    # data = data_one_db.find({'symbol': 'SH600418'})
    # f = stock_db.find({"use": 1})
    f = stock_db.find({"use": 1})
    
    for i in f:
        # if 'thirty_time_list' not in i:
            
            x = history_five_db.find_one({"symbol": i['symbol']})
            if not x:
                print(i['symbol'])
    # count = 0
    # for i in data:
        
    #     if i['item'][-1][0] < 1634003090000:
    #         print(i['symbol'])
    #         count += 1
    # print(count)

    # for i in data:
    #     son_work_together_one_mintue2(i, f, 1634088600)

def son_work_together_one_mintue2(x, stock, timestamp):
    symbol = x['symbol']
    item = x['item']
    history = history_five_db.find_one({"symbol": symbol})
    
    end_price = stock['end_price']

    lol = (item[-1][5] - end_price) % end_price * 100
    if lol > 7:
        stock_tmp_db.update({"symbol": symbol}, {"$set": {"today": 0}}, upsert=True)
    elif lol < -7:
        stock_tmp_db.update({"symbol": symbol}, {"$set": {"today": 1}}, upsert=True)

    five_time_list = history['five_time_list']
    five_time_dict = history['five_time_dict']
    five_max = int(five_time_dict[five_time_list[-1]])

    fifteen_time_list = history.get('fifteen_time_list', [])
    fifteen_time_dict = history.get('fifteen_time_dict', {})
    fifteen_max = int(fifteen_time_dict[fifteen_time_list[-1]])

    # thirty_time_list = history['thirty_time_list']
    # thirty_time_dict = history['thirty_time_dict']
    
    r = len(item)
    msg_list = []

    flag = False
    count5= 0
    for end_index in range(r-1, 0, -1):
        if count5 > 0:
            count5 -= 1
            continue
        ts = int(item[end_index][0] / 1000)
        if ts < timestamp:
            break
        head_index = end_index - 4
        tmp5 = 0
        if item[end_index][5] - item[head_index][2] <= 0:
            continue
        for x in range(head_index, end_index+1):
            tmp5 += item[x][9]
        p = round(tmp5 / five_max, 2)
        if p >= 2:
            # query_find = record_db.find_one({"symbol": symbol, "ts": ts, 'type': 5})
            # if not query_find:
            #     flag = True
            #     record_db.insert_one({"symbol": symbol, "ts": ts, 'vol': tmp5, "p": p, 'type': 5})
            # else:
            #     break
            if end_index + 5 >= r:
                flag = True
            count5 = 5
            msg_list.append(f"{time.strftime('%H:%M', time.localtime(ts))}#{p}倍#5")

    count15= 0
    for end_index in range(r-1, 0, -1):
        if count15 > 0:
            count15 -= 1
            continue
        ts = int(item[end_index][0] / 1000)
        if ts < timestamp:
            break
        head_index = end_index - 14
        tmp5 = 0
        if item[end_index][5] - item[head_index][2] < 0:
            continue
        for x in range(head_index, end_index+1):
            tmp5 += item[x][9]
        p = round(tmp5 / fifteen_max, 2)
        if p >= 2:
            # query_find = record_db.find_one({"symbol": symbol, "ts": ts, 'type': 15})
            # if not query_find:
            #     flag = True
            #     record_db.insert_one({"symbol": symbol, "ts": ts, 'vol': tmp5, "p": p, 'type': 5})
            # else:
            #     break
            if end_index + 5 >= r:
                flag = True
            count15 = 7
            msg_list.append(f"{time.strftime('%H:%M', time.localtime(ts))}#{p}倍#15")
    if flag:
        # msg_list = query_find = record_db.find({"symbol": symbol, "ts": {"$gte": NINE_TIME_STAMPi}})
        msg = ''
        for i in msg_list:
            # msg = msg + "- " + f"{time.strftime('%H:%M', time.localtime(i['ts']))}#{i['p']}#{i['type']}" + '\n'
            msg = msg + "- " + i + '\n'
        if msg:
            send_s_msg(symbol, msg, history['name'])
            send_info(symbol, msg)

def aaa():
    all = history_five_db.find()
    for i in all:
        a = i['five_time_list']
        a1 = i['five_time_dict']
        b = i['fifteen_time_list']
        b1 = i['fifteen_time_dict']
        c = i.get('thirty_time_list', [])
        c1 = i.get('thirty_time_dict', {})
        if "2021-10-26" in a1:
            a1.pop('2021-10-26')
        if "2021-10-26" in a:
            a.remove("2021-10-26")
        
        if "2021-10-26" in b1:
            b1.pop('2021-10-26')
        if "2021-10-26" in b:
            b.remove("2021-10-26")
        
        if "2021-10-26" in c1:
            c1.pop('2021-10-26')
        if "2021-10-26" in c:
            c.remove("2021-10-26")
        print(i['symbol'])
        history_five_db.update({"symbol": i['symbol']}, 
        {"$set": 
        {
            "five_time_list": a,
            "five_time_dict": a1,
            "fifteen_time_list": b,
            "fifteen_time_dict": b1,
            "thirty_time_list": c,
            "thirty_time_dict": c1,
        }}, upsert=True)

def dd(sy="SZ002484", ts=1634607000):
    f = record_db.find({"symbol": sy, "ts": {"$gte": ts}}).sort([('ts', 1)])
    pp = {'symbol': sy}
    p5 = []
    t5 = []
    p15 = []
    t15 = []
    pp5 = {}
    pp15 = {}
    for i in f:
        if 'p5' in i:
            # p5.append({i['ts']: i['p5']})
            pp5[i['ts']] = i['p5']
            # p5.append(i['p5'])
            # t5.append(i['ts'])
        if 'p15' in i:
            # p15.append(i['p15'])
            # t15.append(i['ts'])
            # p15.append({i['ts']: i['p15']})
            pp15[i['ts']] = i['p15']
    pp['p5'] = p5
    pp['t5'] = t5
    pp['p15'] = p15
    pp['t15'] = t15
    pp['pp5'] = pp5
    pp['pp15'] = pp15
    print(pp)


def  get_hot_ip_job():
    count = settings_db.find_one({"count": 1})
    end = new_ip_obj.get_req(int(count['end']))
    print(end)
    settings_db.update_one({"count": 1}, {"$set": {'end': end}}, upsert=True)


def setss():
    all = stock_db.find({"use": 1}) # , 'symbol': 'SH600170'
    for i in all:
        d = data_day_db.find_one({"symbol": i["symbol"]})
        set_all_stock_tmp(d, i, 1)

def seeet():
    all = stock_db.find({"today": 7})
    for i in all:
        record_db.delete_many({"symbol": i["symbol"]})

def sendmsg():
    stock = stock_db.find_one({"symbol": 'SH603722'})
    send_s_msg(stock, '愚蠢的居居哟', ding_url0)


def set_all_stock_tmpxxx():
    ff = stock_db.find({"use": 1})
    count = 0
    count1 = 0
    count2 = 0
    count3 = 0
    for stock in ff:
        # -4 195 467 802 1188
        # -3 193 464 799 1185
        # -2 185 454 789 1175
        # -1 194 466 803 1188
        x = data_day_db.find_one({"symbol": stock['symbol']})
        item = x['item']
        print(stock['symbol'])
        last = item[-4]
        if stock.get("use", 0):
            if last[7] > -9:
                msg = stock['gsjj'] + stock['hxtc'] + stock['jyfw']
                f = 0
                for k in KEYS:
                    if k in msg:
                        f = 1
                        break
                if f:
                    dd = data_day_db.find_one({"symbol": x['symbol']})
                    ct = 0
                    ct2 = 0
                    ct3 = 0
                    ct4 = 0
                    for d in dd['item'][-10:]:
                        if d[7] <= 2:
                            ct += 1
                        if d[7] <= 1:
                            ct2 += 1
                        if d[7] <= 0.5:
                            ct3 += 1
                        if d[7] <= 0:
                            ct4 += 1
                    if ct < 7:
                        count += 1
                    if ct2 < 7:
                        count1 += 1
                    if ct3 < 7:
                        count2 += 1
                    if ct4 < 7:
                        count3 += 1
  
    print(count, count1, count2, count3)


def check_tmp():
    all_stocks = stock_db.find()
    for i in all_stocks:
        if not i.get('gsjj'):
            print(i)

def get_record_data():
    ts = settings_db.find_one({"name": "nine_timestamp"})['timestamp']
    all = record_db.find({"ts": {"$gte": ts}})

    kk = {}
    for i in all:
        i.pop("_id")
        i['date'] = time.strftime('%Y-%m-%d %H:%M', time.localtime(i['ts']))
        if i['symbol'] not in kk:
            # print(i['symbol'])
            kk[i['symbol']] = []
        if len(kk[i['symbol']]) < 3:
            xxx = stock_db.find_one({"symbol": i['symbol']})
            i['name'] = xxx['name']
            i['important'] = xxx['high']
            kk[i['symbol']].append(i)
            # print(i)
    count = 0
    count1 = 0
    for k, v in kk.items():
        if v[0]['date'] > '2021-11-10 09:40' and v[0]['date'] < '2021-11-10 11:40':
            print(k)
            count += 1
            if v[0]['important']:
                count1 +=1
            for zz in v:
                print(zz)
    print(count, count1)



def fifteen_get_five():
    fif = record_db.find({"p15": {"$gte": 2}, "ts": {"$gte": 1635696000}})
    all_fif = []
    for i in fif:
        if i['symbol'] not in all_fif:
            all_fif.append(i['symbol'])
            five = record_db.find({"p5": {"$gte": 2}, 'ts': {"$gte": i['ts']}, 'symbol': i['symbol']}).sort([('ts', 1)])
            ll = []
            print(time.strftime('%Y-%m-%d', time.localtime(i['ts'])))
            for it in five:
                tt = time.strftime('%Y-%m-%d', time.localtime(it['ts']))
                if tt not in ll:
                    ll.append(tt)
                    t2 = time.strftime('%H:%M', time.localtime(it['ts']))
                    print(time.strftime('%Y-%m-%d %H:%M', time.localtime(it['ts'])), i['symbol'])


def get_five_now_data():
    ts = settings_db.find_one({"name": "nine_timestamp"})['timestamp']
    all = record_db.find({"ts": {"$gte": ts}, 'high': 1}).sort([('ts', 1)])

    kk = []
    for i in all:
        i.pop("_id")
        i['date'] = time.strftime('%Y-%m-%d %H:%M', time.localtime(i['ts']))
        if i['symbol'] not in kk:
            print(i)
            kk.append(i['symbol'])

def save_five_days_record():
    st = data_day_db.find_one({'symbol': 'SZ002466'})
    ts = st['item'][-10][0] / 1000
    all = record_db.find({"ts": {"$gte": ts}}).sort([('ts', 1)])
    record_day_db.delete_many({"date": {"$gte": '1'}})
    ll = []
    for i in all:
        datte = time.strftime('%Y-%m-%d', time.localtime(i['ts']))
        record_day_db.update_one({"symbol": i['symbol'], 'date': datte}, {"$set": {"symbol": i['symbol'], 'date': datte}}, upsert=True)
        if i['symbol'] not in ll:
            ll.append(i['symbol'])
    print(len(ll))

    # for i in ll:
    #     pr = stock_db.find_one({"symbol": i})
    #     if pr['price'] < 2000000000:
    #         record_db.delete_many({"symbol": i})

def save__record_to_tmp():
    st = data_day_db.find_one({'symbol': 'SZ002466'})
    ts = st['item'][-1][0] / 1000
    all = record_db.find({"ts": {"$gte": ts}}).sort([('ts', 1)])
    p = []
    for i in all:
        i.pop('_id')
        p.append(i)
    record_tmp_db.insert_many(p)


def mamama():
    st = data_day_db.find_one({"symbol": 'SH600170'})


    x1 = EMA([round(decimal.Decimal(x[5]), 2) for x in st['item'][:-2]])
    

def fix():
    pass


if __name__ == "__main__":
    # set_five_history()
    # set_history()
    # rettt()
    # get_name()
    # ccccc()
    
    # set_ip_count()
    # fix()
    # set_five_history()
    # compare()
    # set_ct()


    # update_account()
    # tet()
    # reset_all_ip()


    ######################## IPIPIPIP
    # iiippp.parse()
    # pp.save()

    # send_req("121.232.148.208:3256")

    # play()
    

    # fix()


    # send_info('SH600096', 'teqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqst')
    # send_s_msg('SH600096', 'teqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqst')

    # update_stock_core()

    # get_day_data()
    # run()
    # t1 = time.time()
    # test_son_work_together_one_mintue()
    # print(time.time()-t1)
    # new_ip_obj.get_req(15363, 15374)
    clean_db_ip()
    clean_db_txt_ip()
    clean_dirty_db_ip()
    # new_ip_obj.save()
    # new_ip_obj.save_dirty() 
    # check_duplicate_ip()
    # set_account()
    # saveAccount()
    # update_account()
    # get_name() 
    # dd()
    # aaa()
    # get_hot_ip_job()
    # sset4()
    # sset3()
    # sset2()
    # sset()
    # play_without_flag(mission_five)
    # play_without_flag_for_all(mission_day_all_stock)
    # seeet()
    # sendmsg()
    # set_all_stock_tmpxxx()
    # check_tmp()
    #  get_record_data()
    # get_record_datasad(1636300800, 1636300800 + 24*3600)
    # get_record_histroy1()
    # get_record_histroy2()
    # get_record_histroy3()
    # get_record_histroy4()
    # get_record_histroy5()
    # fifteen_get_five()
    # save__record_to_tmp()

    
    # setss()

    # get_five_now_data()


    # save_five_days_record()

    # mamama()
    # stand_v2()
    # mian_do()

    